import config from '@/utils/config.js';
import {
	getToken
} from '@/utils/auth'
import {
	showToast,
	showConfirm
} from '@/utils/util.js';
import errorCode from '@/utils/errorCode'
const baseUrl = config.websiteUrl
let timeout = 10000
export const request = config => {

	const isToken = (config.headers || {}).isToken === false
	config.header = config.header || {}
	if (getToken() && !isToken) {
		config.header['Authorization'] = 'Bearer ' + getToken()
	}
	// get请求映射params参数
	if (config.params) {
		let url = config.url + '?' + tansParams(config.params)
		url = url.slice(0, -1)
		config.url = url
	}
	return new Promise((resolve, reject) => {
		uni.request({
			method: config.method || 'post',
			timeout: config.timeout || timeout,
			url: baseUrl + config.url,
			data: config.data,
			header: config.header,
			dataType: 'json'

		}).then(response => {
			// debugger
			let res = response
			// showToast('pags')
			const code = res.data.code || 200
			if (code === 401) {
				showConfirm('登录状态已过期，您可以继续留在该页面，或者重新登录?').then(res => {
					if (res.confirm) {

					}
				})
				reject('无效的会话，或者会话已过期，请重新登录。')
			}
			resolve(res.data)
		})

	})
}